﻿package mortal.game.view.shop
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.geom.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.shopMall.data.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class BuyGiftItemWin extends SmallWindow
    {
        private var _buyItemPart:BuyAdvanceItemPart;
        private var _buyGiftPart:BuyAdvanceGiftPart;
        private var _openGiftPart:BuyAdvanceOpenGiftPart;
        private var _buyItemMoneyPart:BuyItemMoneyPart;
        private var _buyGiftPartTxt:GTextFiled;
        private var _rechargeTip:GTextFiled;
        private var _bg:GImageBitmap;
        private var _btnRecharge:GLoadingButton;
        private var _autoBuy:GCheckBox;
        private var _showType:int;
        private var _rect3d:Rect3DObject;
        public static var isAutoBuy:Boolean = false;
        public static var targetItemData:ItemData;
        public static var isNeedTobuyCurTime:Boolean = false;
        private static var _instance:BuyGiftItemWin;

        public function BuyGiftItemWin(param1:ILayer = null)
        {
            super(param1);
            return;
        }// end function

        public function setBuyData(param1:int, param2:int, param3:int, param4:int = 0, param5:int = 0) : void
        {
            targetItemData = new ItemData(param1);
            this._showType = param2;
            if (this._showType == 1)
            {
                this.setSize(280, 500);
            }
            else
            {
                this.setSize(280, 574);
            }
            this.show(param4, param5);
            this._buyItemPart.data = targetItemData;
            this._buyItemPart.num = param3;
            this.updatePos();
            Dispatcher.dispatchEvent(new DataEvent(EventName.BestGetRebatShopConfig));
            NetDispatcher.addCmdListener(ServerCommand.RebateShopGetSOnsellItemsMsg, this.updateGiftPark);
            NetDispatcher.addCmdListener(ServerCommand.RebateShopGetSOnsellPlayerMsg, this.updateGiftPark);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.updateGiftPark);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            if (!this._rect3d)
            {
                this._rect3d = Rect3DManager.instance.registerWindow(true, new Rectangle(0, 0, 0, 0), this);
            }
            Rect3DManager.instance.windowShowHander(null, this);
            this._bg = UIFactory.gImageBitmap("", 0, 0, this);
            this._buyItemPart = UICompomentPool.getUICompoment(BuyAdvanceItemPart);
            this.addChild(this._buyItemPart);
            this._buyItemMoneyPart = UICompomentPool.getUICompoment(BuyItemMoneyPart);
            this.addChild(this._buyItemMoneyPart);
            var _loc_1:* = GlobalStyle.textFormatHuang2.center();
            _loc_1.size = 20;
            this._buyGiftPartTxt = UIFactory.gTextField("", 0, 0, 130, 30, this, _loc_1);
            _loc_1 = GlobalStyle.textFormatLv;
            this._rechargeTip = UIFactory.gTextField(Language.getString(35535), 0, 0, 210, 30, this, _loc_1);
            this._autoBuy = UIFactory.checkBox(Language.getString(35536), 24, 43, 210, 28, this);
            this._autoBuy.selected = isAutoBuy;
            this._autoBuy.configEventListener(Event.CHANGE, this.selectedChangeHandler);
            this._btnRecharge = UIFactory.gLoadingButton(ResFileConst.RechargeBtn, 0, 0, 114, 33, this);
            this._btnRecharge.configEventListener(MouseEvent.CLICK, this.rechargeMoney);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.RebateShopGetSOnsellItemsMsg, this.updateGiftPark);
            NetDispatcher.removeCmdListener(ServerCommand.RebateShopGetSOnsellPlayerMsg, this.updateGiftPark);
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.updateGiftPark);
            if (this._rect3d)
            {
                Rect3DManager.instance.disposeRect3d(this._rect3d);
                this._rect3d = null;
            }
            this._bg.dispose(param1);
            this._bg = null;
            this._buyItemPart.dispose(param1);
            this._buyItemPart = null;
            this._autoBuy.dispose(param1);
            this._autoBuy = null;
            if (this._buyGiftPart)
            {
                this._buyGiftPart.dispose(param1);
                this._buyGiftPart = null;
            }
            if (this._openGiftPart)
            {
                this._openGiftPart.dispose(param1);
                this._openGiftPart = null;
            }
            this._buyGiftPartTxt.dispose(param1);
            this._buyGiftPartTxt = null;
            this._buyItemMoneyPart.dispose(param1);
            this._buyItemMoneyPart = null;
            this._btnRecharge.dispose(param1);
            this._btnRecharge = null;
            this._rechargeTip.dispose(param1);
            this._rechargeTip = null;
            targetItemData = null;
            isNeedTobuyCurTime = false;
            isAutoBuy = false;
            super.disposeImpl(param1);
            return;
        }// end function

        private function selectedChangeHandler(event:Event) : void
        {
            isAutoBuy = this._autoBuy.selected;
            return;
        }// end function

        private function addBuyGiftPart() : void
        {
            if (this._buyGiftPart == null)
            {
                this._buyGiftPart = UICompomentPool.getUICompoment(BuyAdvanceGiftPart);
                this.addChild(this._buyGiftPart);
            }
            return;
        }// end function

        private function addOpenGiftPart() : void
        {
            if (this._openGiftPart == null)
            {
                this._openGiftPart = UICompomentPool.getUICompoment(BuyAdvanceOpenGiftPart);
                this.addChild(this._openGiftPart);
            }
            return;
        }// end function

        private function rechargeMoney(event:MouseEvent) : void
        {
            GameUtil.gotoRecharge(ModuleTypeConst.BuyGiftModule);
            return;
        }// end function

        public function updateGiftPark(param1:Object = null) : void
        {
            var _loc_2:ItemData = null;
            var _loc_3:Boolean = false;
            var _loc_5:TShopPanicBuyItem = null;
            var _loc_6:RebateItemData = null;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            if (!_loc_3)
            {
                _loc_2 = GiftsUtil.isCacnUseGiftByGiftType(EGiftBag._EGiftBagCost, targetItemData.itemInfo.group, targetItemData.itemInfo.category, targetItemData.itemInfo.type);
                if (_loc_2)
                {
                    _loc_3 = true;
                }
            }
            var _loc_4:* = ShopConfig.instance.rebateShopMap[Cache.instance.shop.curShopId];
            if (!_loc_3)
            {
                if (_loc_4 && _loc_4.length)
                {
                    _loc_5 = _loc_4[0];
                    _loc_3 = this.checkHasGiftCanBuy(_loc_5.itemCode, targetItemData.itemInfo.group, targetItemData.itemInfo.category, targetItemData.itemInfo.type);
                    if (_loc_3)
                    {
                        _loc_6 = Cache.instance.shop.getRebateInfoDicByItemCode(_loc_5.itemCode);
                        if (_loc_6 && _loc_6.remainInfo)
                        {
                            _loc_3 = _loc_6.remainInfo.leftAmount > 0;
                        }
                        if (_loc_3 && _loc_6 && _loc_6.playerInfo)
                        {
                            _loc_3 = _loc_6.playerInfo.amount < _loc_5.buyLimit;
                        }
                    }
                }
            }
            if (!_loc_3)
            {
                if (_loc_4 && _loc_4.length)
                {
                    _loc_7 = _loc_4.length;
                    _loc_8 = 1;
                    while (_loc_8 < _loc_7)
                    {
                        
                        _loc_5 = _loc_4[_loc_8];
                        _loc_3 = this.checkHasGiftCanBuy(_loc_5.itemCode, targetItemData.itemInfo.group, targetItemData.itemInfo.category, targetItemData.itemInfo.type);
                        if (_loc_3)
                        {
                            break;
                        }
                        else
                        {
                            _loc_5 = null;
                        }
                        _loc_8++;
                    }
                }
            }
            if (_loc_3)
            {
                this._buyGiftPartTxt.text = "";
                if (_loc_5)
                {
                    this.addBuyGiftPart();
                    this._buyGiftPart.data = _loc_5;
                }
                else if (this._buyGiftPart)
                {
                    this._buyGiftPart.dispose();
                    this._buyGiftPart = null;
                }
                if (_loc_2)
                {
                    this.addOpenGiftPart();
                    this._openGiftPart.data = _loc_2;
                }
                else if (this._openGiftPart)
                {
                    this._openGiftPart.dispose();
                    this._openGiftPart = null;
                }
                if (isNeedTobuyCurTime && BuyGiftItemWin.isViewShow)
                {
                    if (this._buyGiftPart)
                    {
                        this._buyGiftPart.buyItemHandler();
                    }
                    else if (this._openGiftPart)
                    {
                        this._openGiftPart.buyItemHandler();
                    }
                }
            }
            else
            {
                if (isNeedTobuyCurTime)
                {
                    MsgManager.showMouseMsg(Language.getString(30414), MsgRollTipsType.msgRollTips3);
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShopBuyItemFail, null));
                }
                if (this._buyGiftPart)
                {
                    this._buyGiftPart.dispose();
                    this._buyGiftPart = null;
                }
                if (this._openGiftPart)
                {
                    this._openGiftPart.dispose();
                    this._openGiftPart = null;
                }
                this._buyGiftPartTxt.text = Language.getString(35537);
            }
            isNeedTobuyCurTime = false;
            this.updatePos();
            return;
        }// end function

        private function checkHasGiftCanBuy(param1:int, param2:int, param3:int, param4:int) : Boolean
        {
            var _loc_7:Array = null;
            var _loc_9:ItemInfo = null;
            var _loc_10:Boolean = false;
            var _loc_11:int = 0;
            if (!Cache.instance.shop.isGetRebatInfoByItemCode(param1))
            {
                return false;
            }
            var _loc_5:* = ItemConfig.instance.getConfig(param1) as ItemGiftBagInfo;
            var _loc_6:* = _loc_5.itemList.split("&");
            var _loc_8:* = _loc_6.length;
            while (_loc_11 < _loc_8)
            {
                
                if (_loc_6[_loc_11])
                {
                    _loc_7 = _loc_6[_loc_11].split("#");
                    _loc_9 = ItemConfig.instance.getConfig(int(_loc_7[1]));
                    if (_loc_9.group == param2 && _loc_9.category == param3 && _loc_9.type == param4)
                    {
                        _loc_10 = true;
                        break;
                    }
                }
                _loc_11++;
            }
            return _loc_10;
        }// end function

        public function updatePos() : void
        {
            if (this._showType == 1)
            {
                this._bg.imgUrl = ImagesConst.BuyItemAndGiftWin_short;
                this._bg.x = 20;
                this._bg.y = 40;
                this._buyItemPart.x = 30;
                this._buyItemPart.y = 90;
                if (this._buyGiftPart)
                {
                    this._buyGiftPart.x = 30;
                    this._buyGiftPart.y = 250;
                }
                if (this._openGiftPart)
                {
                    this._openGiftPart.x = 30;
                    this._openGiftPart.y = 250;
                }
                this._autoBuy.x = 72;
                this._autoBuy.y = 358;
                this.addChild(this._autoBuy);
                this._buyItemMoneyPart.x = 30;
                this._buyItemMoneyPart.y = 384;
                this._buyGiftPartTxt.x = 84;
                this._buyGiftPartTxt.y = 300;
                this._btnRecharge.x = 88;
                this._btnRecharge.y = 442;
                this._rechargeTip.x = 30;
                this._rechargeTip.y = 476;
            }
            else if (this._showType == 2)
            {
                this._bg.imgUrl = ImagesConst.BuyItemAndGiftWin_long;
                this._bg.x = 18;
                this._bg.y = 40;
                this._buyItemPart.x = 30;
                this._buyItemPart.y = 100;
                if (this._buyGiftPart)
                {
                    this._buyGiftPart.x = 30;
                    this._buyGiftPart.y = 277;
                }
                if (this._openGiftPart)
                {
                    this._openGiftPart.x = 30;
                    this._openGiftPart.y = 277;
                }
                this._autoBuy.x = 66;
                this._autoBuy.y = 475;
                this.addChild(this._autoBuy);
                this._buyItemMoneyPart.x = 30;
                this._buyItemMoneyPart.y = 424;
                this._buyGiftPartTxt.x = 84;
                this._buyGiftPartTxt.y = 327;
                this._btnRecharge.x = 88;
                this._btnRecharge.y = 511;
                this._rechargeTip.x = 30;
                this._rechargeTip.y = 549;
            }
            return;
        }// end function

        public static function get instance() : BuyGiftItemWin
        {
            if (_instance == null)
            {
                _instance = new BuyGiftItemWin;
                _instance.layer = LayerManager.windowLayer3D;
            }
            return _instance;
        }// end function

        public static function get isViewShow() : Boolean
        {
            return _instance && !_instance.isHide;
        }// end function

    }
}
